<?php
require_once APP . '/application/controllers/UserAction.php';
require_once(APP . '/application/models/MeetModel.php');
require_once(APP . '/application/models/Article.php');
require_once(APP . '/application/models/UserModel.php');

/**
 * 会议页面
 * @package admin
 */
class MeetController extends UserAction
{
	/**
	 * 会议编号
	 * @var Integer
	 */
    private $mid;
    /**
     * 域名访问
     * @var String
     */
	private $domain;
	/**
	 * 会议信息
	 * @var Array
	 */
	private $minfo;
	
	/**
	 * 会议是否允许访问
	 * @var Boolean
	 */
	private $canshow = false;
	
	
    public function init()
    {
        $mid = (int)$this->getParam("mid");
        
        if ($this->getParam("email") != null)
       		$_SESSION['iemail'] = $this->getParam("email");
        
        if ($this->getParam("d") != null){
        	setcookie("d", 1, null, "/");
        	$this->canshow = true;
        }
        
        if ($this->getCookie("d") != null){
        	$this->canshow = true;
        }
        
        $this->mid = $mid;

        $domain = $this->getParam("domain");
         
        $obj_meet = new MeetModel($this->db);
        if (!empty($domain)){
       		$arr_minfo = $obj_meet->showByDomain($domain);
       		if(empty($arr_minfo)){
       			header("location:" . $this->view->fullUrl);
       			exit;
       		}
        }else
        	$arr_minfo = $obj_meet->show($mid);
        
        $this->mid = $arr_minfo['mid'];
        $this->domain = $arr_minfo['domain'];
        $this->minfo = $arr_minfo;
        
        if ($this->getParam("lan") != null)
        	$_SESSION['lan' . $this->mid] = $this->getParam("lan");
        
        if ($_SESSION['lan' . $this->mid] == 'en')
        	$this->view->plan = 'en';
        else
        	$this->view->plan = '';
        
        if ($arr_minfo['status'] == 3 && $arr_minfo['del'] == 0)
        	$this->canshow = true;
        
        $admin = false;
        if ($arr_minfo['uid'] == $_SESSION['uid'])
        	$admin = true;
        
        if (isset($_SESSION['ADMIN_ID']))
        	$admin = true;
        
        if ($this->canshow == false && $admin == false){
        	if ($this->view->plan == 'en')
        		exit('Conference is not verified.');
        	else
        		exit("会议还未通过审核!");
        }
        
        $this->view->mid = $this->mid;
        $this->view->domain = $this->domain;
        $this->view->minfo = $arr_minfo;
        
        $this->view->selected = $this->getParam("p");
        if (empty($this->view->selected))
        	$this->view->selected = 'index';
        

    }
    
    /**
     * 获取当前语言
     * @return string
     */
    private function getLan()
    {
    	if ($_SESSION['lan' . $this->mid] == 'en')
    		return "en";
    	else
    		return "";
    }
    
    
    
    /**
     * 首页
     */
    public function indexAction()
    {
    	$arr_top = unserialize($this->minfo['top']);
    	$arr_left = unserialize($this->minfo['left']);
    	$cuid = (int)$this->getParam("cuid");
    	
    	// 最终确认
    	if ($cuid){
    		$msg = '您已完成最终参会确认。';
    		$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?lan=cn';
    		
    		if ($this->getLan() == 'en'){
    			$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?lan=en';
    			$msg = 'Final confirmation is completed.';
    		}
    		
    		if ($this->minfo['enddate'] < date("Y-m-d H:i:s")){
    			if ($this->getLan() == 'en'){
    				$msg = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_en'");
    			}else{
    				$msg = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_cn'");
    			}
    		}else{    		
    			$this->db->update("meetuser", array("ischeck" => 4), "mid={$this->mid} and uid={$cuid} and ischeck=2");
    		}
    				
			$this->showtipmessage( $url, $msg);
		}
    	
    	$code = $this->getParam("code");// 邀请码
    	if ($code != NULL)
    		$_SESSION['m_invitecode'] = $code;
    	
		$arr_menu = $this->getMenuByUUID($this->minfo['homeuuid'], "");
    	
    	
    	if (empty($arr_menu)){
    		$this->regAction();
    	}else{
    		if ($arr_menu['type'] == 'file'){
    			$this->setParam("uuid", $arr_menu['uuid']);
    			$this->fileAction();
    		}elseif($arr_menu['type'] == 'login'){
    			$this->regAction();	
    		}else{
    			$this->setParam("uuid", $arr_menu['uuid']);
    			$this->articleAction();
    		}
    	}
    }
    
    /**
     * 文章显示
     */
    public function articleAction()
    {
	    $uuid = $this->getParam("uuid");
	    
	    $menu = $this->getMenuByUUID($uuid, "article");
	    $aid = $menu['aid'];
	    if ($this->getLan() == 'en')
	    	$aid = $menu['enaid'];
	    
	    $content = $this->db->fetchOne("select content from marticle where aid=? and mid=?", array($aid, $this->mid));

	    $this->view->content = $content;
    	$this->render("article");
    }
    
    
    /**
     * 文件上传
     */
    public function fileAction()
    {
    	$this->checkLogin(true);
    	
    	$uuid = $this->getParam("id");
    	
    	$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=mfile';
    	$msg = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_cn'");
    	
    	if ($this->getLan() == 'en'){
    		$msg = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_en'");
    	}
    	
    	if ($this->minfo['regend'] < date("Y-m-d H:i:s")){
    		$this->showtipmessage($url, $msg);
    		return;
    	}
    	
    	
    	$this->view->uuid = $uuid;
    	$this->render("file");
    }
    
    
    /**
     * 上传文件处理
     */
    public function dofileAction()
    {
    	$this->checkLogin(true);
    	
    	$uuid = $this->getParam("uuid");

    	$top = $this->minfo['top'];
    	$top = unserialize($top);
    	$left = $this->minfo['left'];
    	$left = unserialize($left);
    	$menu_all = array_merge($top, $left);
    	
    	$arr_menu = array();
    	foreach($menu_all as $v){
    		if ($v['uuid'] == $uuid && $v['type'] == 'file'){
    			$arr_menu = $v;
    			break;
    		}
    	}
    	
    	if (empty($arr_menu)){
    		exit("no uuid");
    	}
    	
    	$nowd = date("YmdHis");
    	if ($this->getLan() == 'en')
    		$filename = "{$arr_menu['enname']}_{$this->uid}_{$this->uinfo['enlname']} {$this->uinfo['enfname']}_$nowd"  ;
    	else
    		$filename = "{$arr_menu['name']}_{$this->uid}_{$this->uinfo['enlname']} {$this->uinfo['enfname']}_$nowd"  ;
    	 
    	$upfile = Util::upmeetfile("file", $this->mid, $filename);
    		
    	if (empty($upfile)){
    		$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=file&id=' . $uuid;
    		header("location:" . $url);
    		exit;
    	}
    	
    	// 插入记录
    	$exists_filename = $this->db->fetchOne("select filename from mfile where mid=? and uid=? and uuid='?'", array($this->mid, $this->uid, $uuid));
    	if (!empty($exists_filename)){
    		$isWin = false;
    		if (preg_match("/win/i", PHP_OS) != 0)
    			$isWin = true;
    		
    		if ($isWin)
    			unlink(iconv("utf-8", "gbk", APP . $exists_filename));
    		else
    			unlink(APP . $exists_filename);
    		
    		$this->db->update("mfile", array("filename" => $upfile, "fdate" => date("Y-m-d H:i:s")), "mid={$this->mid} and uid={$this->uid} and uuid='{$uuid}'");
    	}else{
    		$values = array("mid" => $this->mid, "uid" => $this->uid, "uuid" => $uuid, "filename" => $upfile, "fdate" => date("Y-m-d H:i:s"));
    		$this->db->insert("mfile", $values);
    	}
    
    	// 跳转到文件管理界面
    	$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=mfile';
    	header("location:" . $url);
    	exit;
    }
    
    
    /**
     * 显示注册登录界面  平台注册界面
     */
    public function regAction()
    {
    	$uid = (int)$_SESSION['uid'];
    	
    	
    	// 如果已经登录，则到登录后的页面
    	if ($uid != 0){
    		$has = $this->db->fetchOne("select count(*) from meetuser where mid=? and uid=?", array($this->mid, $uid));
    		if ($has)
    			$url = $this->baseUrl . '/' . $this->minfo['domain'] . '?a=puinfo';
    		else
    			$url = $this->baseUrl . '/' . $this->minfo['domain'] . '?a=muinfo';
    		
    		header("location:" . $url);
    		exit;
    	}
    	
    	
    	// 国家或地区读取
    	$nations = $this->db->fetchOne("select value from platset where name='plat_cn_nations'");
    	$arr_nations = explode("\n", $nations);
    	foreach($arr_nations as $index => $v){
    		$arr_nations[$index] = trim($v);
    		if (empty($arr_nations[$index])){
    			unset($arr_nations[$index]);
    		}
    	}

    	$this->view->dnation = $this->db->fetchOne("select value from platset where name='plat_cn_def_nation'");;
    	$this->view->dennation = $this->db->fetchOne("select value from platset where name='plat_en_def_nation'");;
    	$this->view->css_files = array("js/jquery-ui/jquery-ui-1.8.16.custom.css");
    	$this->view->nations = $arr_nations;
    	$this->view->debug = false;
    	
    	$this->render("reg");
    }


    public function checkinviteAction()
    {
    	$this->checkLogin();
    	
    	$arr_langs = array();
    	$arr_lans['tip2'] = '邀请码错误！';
    	     	 
    	if ($this->getLan() == 'en'){
    		$arr_lans['tip2'] = 'Invitation code error.';
    	}
    	
    	$code = $this->getPost("m_invitecode");
    	
    	if (empty($this->minfo['isinvite']))
    		exit("T");
    	 
    	$row = $this->db->fetchRow("select * from invitecode where mid=? and code='?' and email='?'", array($this->mid, $code, $this->uinfo['email']));
    	if (empty($row)){
    		echo $arr_lans['tip2'];
    	}else
    		echo "T";
    }
    
    /**
     * 用户 登录之后的界面，但可能还未注册会议
     */
    public function muinfoAction()
    {
    	$this->checkLogin();

    	$regitems = $this->minfo['regitems'];
    	$regitems = unserialize($regitems);

    	if (!is_array($regitems))
    		$regitems = array();
    	
    	$arr_langs = array();
    	$arr_lans['tip1'] = '请填写邀请码。';
    	$arr_lans['tip2'] = '邀请码错误！';
    	$arr_lans['tip3'] = "您已做了最终确认，所有信息不可修改。";
    	$arr_lans['tip4'] = "注册时间还未开始。";
    	$arr_lans['tip5'] = "注册系统已经关闭，如有问题请致信".$this->minfo['email']."邮箱。";
    	$arr_lans['tip6'] = "您已完成会议注册，请耐心等待审核结果。";
    	
    	$tip = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_cn'");
    	
    	if (!empty($tip)){
    		$arr_lans['tip5'] = $tip;
    	}
    	
    	
    	if ($this->getLan() == 'en'){
    		$arr_lans['tip1'] = 'Please fill in the invitation code.';
    		$arr_lans['tip2'] = 'Invitation code is incorrect!';
    		$arr_lans['tip3'] = "You have done final confirmation. All information cannot be modified.";
    		$arr_lans['tip4'] = "Registration is to be open.";
    		$arr_lans['tip5'] = "Registration is closed. If you have questions, please sent an e-mail to " . $this->minfo['email'];
    		$arr_lans['tip6'] = "You have completed online registration, and please wait for the verification result.";
    		
    		$tip = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_en'");
    		if (!empty($tip)){
    			$arr_lans['tip5'] = $tip;
    		}
    	}
    	
    	$arr_u_items = array();
    	// 注册
    	if (!empty($_POST))
    	{
    		$codeid = 0;
    		
    		if ($this->minfo['isinvite']){
    			$code = $this->getPost("m_invitecode");
    			if (empty($code)){
    				$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=muinfo';
    				$this->showtipmessage( $url, $arr_lans['tip1']);
    				exit;
    			}
    			
    			$row = $this->db->fetchRow("select * from invitecode where mid=? and code='?' and email='?'", array($this->mid, $code, $this->uinfo['email']));
    			if (empty($row)){
    				$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=muinfo';
    				$this->showtipmessage( $url, $arr_lans['tip2']);
    				exit;
    			}
    			
    			$codeid = $row['codeid'];
    		}
    		
    		
    		$u_regitems = $this->db->fetchOne("select regitems from meetuser where mid=? and uid=?", array($this->mid, $this->uid));
    		$arr_u_items = unserialize($u_regitems);
    		if (!is_array($arr_u_items))
    			$arr_u_items = array();
    		
    		foreach($regitems as $v){
    			if (!$v['show'])continue;
    			if ($v['type'] > 6)continue;// 描述和分割线
    			if (isset($_POST[$v['uuid']]))
    				$arr_u_items[$v['uuid']] = $_POST[$v['uuid']];
    		}
    		
    		$row = $this->db->fetchRow("select * from meetuser where mid=? and uid=?", array($this->mid, $this->uid));
    		if (!empty($row)){
    			if ($row['ischeck'] == 4){
    				$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=muinfo';
    				$this->showtipmessage( $url, $arr_lans['tip3']);
    				exit;
    			}
    			
    			
    			$newValues = array("ischeck" => 1, "regitems" => serialize($arr_u_items), 'regtime' => date("Y-m-d H:i:s"));
    			$this->db->update("meetuser", $newValues, "mid={$this->mid} and uid={$this->uid}");
    		}else{
    			$regstart = strtotime(substr($this->minfo['regstart'], 0,10) . ' 00:00:00');
    			$regend = strtotime(substr($this->minfo['regend'], 0,10) . ' 23:59:59');
    			$now = time();
    			if ($now < $regstart){
    				$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=muinfo';
    				$this->showtipmessage( $url, $arr_lans['tip4']);
    				exit;
    			}
    			if ($now > $regend){
    				$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=muinfo';
    				$this->showtipmessage( $url, $arr_lans['tip5']);
    				exit;
    			}
    			
    			$newValues = array("mid" => $this->mid, "uid" => $this->uid,"ischange" =>0, "ischeck" => 1, "regitems" => serialize($arr_u_items),'oldregitems'=>'', 'regtime' => date("Y-m-d H:i:s"));
    			$this->db->insert("meetuser", $newValues);
    			
    			// 修改邀请码使用状态
    			if ($codeid != 0){
    				$newValues = array('regemail' => $this->uinfo['email'], "uid" => $this->uid, "usetime" => date("Y-m-d H:i:s"));
    				$this->db->update("invitecode", $newValues, "codeid={$codeid}");
    				$_SESSION['m_invitecode'] = "";
    			}
    			
    		}
    		
    		
    		$this->showtipmessage($this->view->fullUrl . '/' . $this->minfo['domain'], $arr_lans['tip6']);
    		return;
    		
    		$this->view->change = true;
    		$this->view->ischeck = 1;
    	}else{
    		$u_regitems = $this->db->fetchOne("select regitems from meetuser where mid=? and uid=?", array($this->mid, $this->uid));
    		$arr_u_items = unserialize($u_regitems);
    		if (!is_array($arr_u_items))
    			$arr_u_items = array();
    	}
    	    	
    	if ($this->minfo['isinvite']){
    		$this->view->icode = $this->db->fetchOne("select code from invitecode where mid=? and email='?'", array($this->mid, $this->uinfo['email']));
    	}
    	
    	$this->view->uitems = $arr_u_items;
    	$this->view->regitems = $regitems;
    	$this->view->selected = "";
    	$this->view->css_files = array("js/jquery-ui/jquery-ui-1.8.16.custom.css");
    	
    	$regend = strtotime(substr($this->minfo['regend'], 0,10) . ' 23:59:59');
    	$now = time();
    	 
    	$this->view->isclose = $now > $regend ? true : false;
    	if ($this->view->isclose && empty($arr_u_items)){
    		$this->view->closemsg = $arr_lans['tip5'];
    	}
    	if (empty($arr_u_items))
    		$this->view->isclose = false;
    	
    	$this->render("muinfo");
    }
    
    /**
     * 平台用户信息界面
     */
    public function puinfoAction()
    {
    	$this->checkLogin();
    	$this->view->selected = "";
    	
    	// 国家或地区读取
    	$nations = $this->db->fetchOne("select value from platset where name='plat_cn_nations'");
    	$arr_nations = explode("\n", $nations);
    	foreach($arr_nations as $index => $v){
    		$arr_nations[$index] = trim($v);
    		if (empty($arr_nations[$index])){
    			unset($arr_nations[$index]);
    		}
    	}
    	
    	$regend = strtotime(substr($this->minfo['regend'], 0,10) . ' 23:59:59');
    	$now = time();
    	
    	$this->view->isclose = $now > $regend ? true : false;
    	$this->view->nations = $arr_nations;
    	$this->view->css_files = array("js/jquery-ui/jquery-ui-1.8.16.custom.css");
    	$this->render("puinfo");
    }
    
    /**
     * 修改密码
     */
    public function pwdAction()
    {
    	$this->checkLogin();
    	$this->view->selected = "";
    	     	 
    	$this->view->css_files = array("js/jquery-ui/jquery-ui-1.8.16.custom.css");
    	$this->render("pwd");
    }
    
    /**
     * 文件管理界面
     */
    public function mfileAction()
    {
    	$this->checkLogin();
    	$this->view->selected = "";
    	
    	$fid = (int)$this->getParam("fid");
    	if ($fid){
    		
    		$url = $this->view->fullUrl . '/' . $this->minfo['domain'] . '?a=mfile';
    		$msg = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_cn'");
    		 
    		if ($this->getLan() == 'en')
    			$msg = $this->db->fetchOne("select value from platset where name='meet_{$this->mid}_ctip_en'");
    		 
    		if ($this->minfo['enddate'] < date("Y-m-d H:i:s")){
    			$this->showtipmessage($url, $msg);
    			return;
    		}
    		
    		$row = $this->db->fetchRow("select * from mfile where fid=?", array($fid));
    		if ($row['uid'] == $this->uid && $row['mid'] == $this->mid){
    			$isWin = false;
    			if (preg_match("/win/i", PHP_OS) != 0)
    				$isWin = true;
    			
    			if ($isWin)
    				unlink(iconv("utf-8", "gbk", APP . $row['filename']));
    			else
    				unlink(APP . APP . $row['filename']);
    			
    			$this->db->delete("mfile", "fid=" . $fid);
    		}
    	}
    	
    	$list = $this->db->fetchAll("select * from mfile where mid=? and uid=? order by fdate asc", array($this->mid, $this->uid));
		foreach($list as $index => $v){
			$list[$index]['item'] = $this->getMenuByUUID($v['uuid']);
		}
    	
    	$this->view->list = $list;
    	$this->render("mfile");
    }
    
    /**
     * 根据编号获取项
     * @param unknown_type $uuid
     * @return Ambigous <multitype:, unknown>
     */
    private function getMenuByUUID($uuid, $type = 'file')
    {
    	$top = $this->minfo['top'];
    	$top = unserialize($top);
    	$left = $this->minfo['left'];
    	$left = unserialize($left);
    	
    	if (!is_array($top))$top = array();
    	if (!is_array($left)) $left = array();
    	$menu_all = array_merge($top, $left);
    	
    	$arr_menu = array();
    	foreach($menu_all as $v){
    		if ($v['uuid'] == $uuid){
    			if ($type != ''){
    				if ($v['type'] == $type){
    					$arr_menu = $v;
    					break;
    				}
    			}else{
    				$arr_menu = $v;
    				break;
    			}
    		}
    	}
    	
    	return $arr_menu;
    }
    
    /**
     * 检测用户登录
     */
    private function checkLogin($ischeck_reg_meet = false)
    {
    	$uid = (int)$_SESSION['uid'];
    	if (empty($uid)){
    		$url = $this->baseUrl . "/" . $this->minfo['domain'] . '?a=reg&p=' . $this->getParam("p");
    		header("location:" . $url);
    		exit;
    	}
    	
    	if ($ischeck_reg_meet){
    		$has = $this->db->fetchOne("select count(*) from meetuser where mid=? and uid=?", array($this->mid, $uid));
    		if (!$has){// 如果还未注册会议，则跳转到注册会议
    			$url = $this->baseUrl . "/" . $this->minfo['domain'] . '?a=muinfo';
    			header("location:" . $url);
    			exit;
    		}
    	}
    	
    	$this->view->ischeck = (int)$this->db->fetchOne("select ischeck from meetuser where mid=? and uid=?", array($this->mid, $uid));
    	$this->uid = $uid;
    	$this->uinfo = $this->db->fetchRow("select * from user where uid=?", array($uid));
    }
}